package com.leo.offer;

/**
 *
 * @author xuexiaolei
 * @version 2018年03月01日
 */
public class T15 {
    public static Node findKthToTail(Node head, int k){
        if (k<1 || head==null){ return null; }
        Node fast=head,low=head;

        for (int i = 0; i < k-1; i++) {
            if (fast.next != null){
                fast = fast.next;
            }else {
                return null;
            }
        }

        while (fast.next != null){
            fast = fast.next;
            low = low.next;
        }
        return low;
    }

    public static void main(String[] args) {
        Node node = Node.createNode(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
        Node kthToTail = findKthToTail(node, 1);
//        Node kthToTail = findKthToTail(node, 2);
//        Node kthToTail = findKthToTail(node, 9);
//        Node kthToTail = findKthToTail(node, 10);
        System.out.println(kthToTail.val);
    }
}
